package studio.raptor.hub.in.server;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import org.apache.http.client.HttpClient;
import studio.raptor.hub.in.ServerProxy;

/**
 * Created by liuyong8 on 2017/7/27.
 */
public class OfferInstProxy extends ServerProxy {
//    http://192.168.1.60:4999/index.php?s=/2&page_id=289
//{
//    "requestObject": {
//    "offerInstId": "270286888430",
//{"requestObject": {"offerInstId": 1000000000002753,"regionId": 8510101,"scopeInfos": [{"scope": "offerInst"},{"scope": "offerInstAttr"},{"scope": "offerInstFeeInfo"}]}}
    private static final String REQ_MESSAGE = "{\"requestObject\": {\"offerInstId\": %s,\"regionId\": %s,\"scopeInfos\": [{\"scope\": \"offerInst\"},{\"scope\": \"offerInstAttr\"},{\"scope\": \"offerInstFeeInfo\"}]}}";

    public OfferInstProxy(HttpClient httpClient) {
        super(httpClient);
    }

    @Override
    protected String handleResponse(String response) {
        DocumentContext sc = JsonPath.parse(response);
        Object object = sc.read("$.resultObject");
        String result = JSON.toJSONString(object, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat);
        if (result.startsWith("{")) {
            result = result.substring(1,result.length()-1);
        }
        return result;
    }

    @Override
    protected String getMessage(Long... params) {
        return String.format(REQ_MESSAGE,params);
    }
}
